package cn.bgs.service.impl;


import cn.bgs.common.AliOss;
import cn.bgs.mapper.MaintenanceMapper;
import cn.bgs.pojo.File;
import cn.bgs.pojo.Maintenance;
import cn.bgs.pojo.ProjectType;
import cn.bgs.service.IMaintenanceService;
import cn.bgs.vo.CellIntermediate;
import cn.bgs.vo.MaintenanceIntermediateVo;
import cn.bgs.vo.MaintenanceListVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

import java.util.Date;

/**
 * <p>
 * 维修记录表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class MaintenanceServiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance> implements IMaintenanceService {
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private AliOss aliOss;
    @Override
    public List<Maintenance> findAll(Integer communityId) {
        return maintenanceMapper.findAll(communityId);
    }


    @Override
    public List<MaintenanceListVo> getMaintenanceList(Integer userId) {
        return maintenanceMapper.getMaintenanceList(userId);
    }

    @Override
    public List<ProjectType> getMaintenceTree() {
        return maintenanceMapper.getMaintenceTree();
    }

    @Override
    public int saveMaintence(MaintenanceIntermediateVo maintenanceIntermediateVo) {
        CellIntermediate cellIntermediate=new cn.bgs.vo.CellIntermediate();
        cellIntermediate.setUserId(maintenanceIntermediateVo.getUserId());
        cellIntermediate.setCommunityId(maintenanceIntermediateVo.getCommunityId());
        cellIntermediate.setBuildingId(maintenanceIntermediateVo.getBuildingId());
        cellIntermediate.setCellId(maintenanceIntermediateVo.getCellId());
        cellIntermediate.setFloorId(maintenanceIntermediateVo.getFloorId());
        cellIntermediate.setPortalId(maintenanceIntermediateVo.getPortalId());
        //添加中间表
        maintenanceMapper.saveCellIntermediate(cellIntermediate);
//        中间表id
        Integer middleId= cellIntermediate.getMiddleId();
        Maintenance maintenance=new Maintenance();
        maintenance.setUserId(maintenanceIntermediateVo.getUserId());
        maintenance.setMiddleId(middleId);
        maintenance.setHouseingName(maintenanceIntermediateVo.getHouseingName());
        maintenance.setMaintenanceProjectsId(maintenanceIntermediateVo.getMaintenanceProjectsId());
        maintenance.setProjectTypeId(maintenanceIntermediateVo.getProjectTypeId());
        maintenance.setTheTitle(maintenanceIntermediateVo.getTheTitle());
        maintenance.setProblemDescription(maintenanceIntermediateVo.getProblemDescription());
        maintenance.setPhone(maintenanceIntermediateVo.getPhone());
        maintenance.setAppointmentTime(maintenanceIntermediateVo.getAppointmentTime());
        //添加维修记录表
        maintenanceMapper.saveMaintence(maintenance);
        //维修记录表主键id
        Integer maintenanceId=maintenance.getMaintenanceId();
//        图片id
        String fileUrl= maintenanceIntermediateVo.getFileUrl();
        return  maintenanceMapper.saveMaintencePhonto(maintenanceId,fileUrl);

    }

    @Override
    public int deleteMaintence(Integer maintenanceId) {
        return maintenanceMapper.deleteMaintence(maintenanceId);
    }


    @Override
    public Boolean into(@Param("theTitle") String theTitle, @Param("problemDescription") String problemDescription, @Param("phone") Integer phone, @Param("appointmentTime") Date appointmentTime) {
        return maintenanceMapper.into(theTitle,problemDescription,phone,appointmentTime);
    }

    @Override
    public Boolean delete(Integer maintenanceId) {
        return maintenanceMapper.delete1(maintenanceId);
    }
    String fileuser;
    @Override
    public String file(MultipartFile file) {
        fileuser = aliOss.upload(file);
        return fileuser;
    }

    @Override
    public List<Maintenance> findById(Integer id) {
        return maintenanceMapper.findById(id);
    }

    @Override
    public Boolean shenhe(@Param("id") Integer id,@Param("shenhe") Integer shenhe) {
        Boolean b  = null;
        if(shenhe == 1){
            String shen = "审核通过";
            b =  maintenanceMapper.shenhe(id,shen);
        }else {
            String shen = "审核不通过";
            b = maintenanceMapper.shenhe(id,shen);
        }
        return b;
    }



}
